package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.rules.admin.service.RuleService;
import com.rules.admin.dao.RuleDao;
import com.rules.admin.entity.Rule;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RuleServiceImpl extends ServiceImpl<RuleDao, Rule> implements RuleService {

   public Rule insert(Rule rule) {
      this.save(rule);
      return rule;
   }

   public Rule update(Rule rule) {
      this.updateById(rule);
      return rule;
   }



   public void deleteByRuleAssembly(long ruleAssemblyId) {
      LambdaQueryWrapper<Rule> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(Rule::getRuleAssemblyId, ruleAssemblyId);
      this.remove(queryWrapper);
   }



   public IPage<Rule> queryPage(long ruleAssemblyId, String name, IPage<Rule> page) {
      LambdaQueryWrapper<Rule> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(Rule::getRuleAssemblyId, ruleAssemblyId);
      if (StringUtils.isNotBlank(name)) {
         queryWrapper.like(Rule::getName,"%" + name + "%");
      }
      return this.page(page, queryWrapper);


   }

   public List<Rule> getRuleListByAssemblyId(long ruleAssemblyId) {
      LambdaQueryWrapper<Rule> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(Rule::getRuleAssemblyId, ruleAssemblyId);
      queryWrapper.orderByDesc(Rule::getPriority);
      queryWrapper.orderByAsc(Rule::getCreateTime);
      return this.list(queryWrapper);
   }

   public List<Rule> getRuleListByAssemblyIdAndStatus(long ruleAssemblyId) {
      LambdaQueryWrapper<Rule> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(Rule::getRuleAssemblyId, ruleAssemblyId);
      queryWrapper.eq(Rule::getRuleStatus,0);
      queryWrapper.orderByDesc(Rule::getPriority);
      queryWrapper.orderByAsc(Rule::getCreateTime);
      return this.list(queryWrapper);
   }
}
